package com.andy.image.ext

import android.net.Uri
import android.widget.ImageView
import com.andy.image.ImageManager
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions

/**
 * Author:Andy
 * Date:2023/4/12 11:49
 * Description:ImageExt
 */
fun ImageView.loadHttpUrl(url: String, placeholder: Int) {
    Glide.with(context)
        .load(url)
        .placeholder(placeholder)
        .error(placeholder)
        .into(this)
}

fun ImageView.loadCircleCropHttpUrl(url: String, placeholder: Int) {
    Glide.with(context)
        .load(url)
        .placeholder(placeholder)
        .error(placeholder)
        .apply(RequestOptions.bitmapTransform(CircleCrop()))
        .into(this)
}

/**
 * @param path eg "file:///android_asset/emoji/image.png"
 */
fun ImageView.loadAssetImage(path: String) {
    Glide.with(context)
        .load(Uri.parse(path))
        .into(this)
}

fun ImageView.loadDrawableRes(id: Int, placeholder: Int) {
    Glide.with(context)
        .load(id)
        .placeholder(placeholder)
        .error(placeholder)
        .into(this)
}

fun ImageView.loadCircleCropDrawableRes(id: Int, placeholder: Int) {
    Glide.with(context)
        .load(id)
        .placeholder(placeholder)
        .error(placeholder)
        .apply(RequestOptions.bitmapTransform(CircleCrop()))
        .into(this)
}

fun ImageView.showAvatar(imageUrl: String?) {
    ImageManager.showAvatar(context, imageUrl, this)
}

